package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 分页查询品牌
     * @return 品牌的分页结果
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", defaultValue = "update_time") String sortBy,
            @RequestParam(value = "desc", defaultValue = "true") boolean desc
    ){
        return ResponseEntity.ok(brandService.queryBrandByPage(key, page, rows, sortBy, desc));
    }

    /**
     * 新增品牌
     * @param brandDTO 品牌对象
     * @return 无
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO){
        brandService.saveBrand(brandDTO);
        // 新增成功，返回201，无返回值，所以用build
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据分类id查询品牌
     * @param id
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(@RequestParam("id") Long id){
        return ResponseEntity.ok(brandService.queryBrandByCategoryId(id));
    }

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id){
        return ResponseEntity.ok(brandService.queryById(id));
    }

    /**
     * 根据品牌id的集合，查询品牌集合
     * @param idList id集合
     * @return 品牌集合
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam ("ids") List<Long> idList){
        return ResponseEntity.ok(brandService.queryByIds(idList));
    }
}
